<?php

class FunctionsHelper {
	public static function includeFiles($arrFiles){
		foreach ($arrFiles as $File)
			include_once($File);
	}

	public static function ReplaceAccentsAndEnie($text){
		$accents = array('á' => 'a', 'é' => 'e', 'í' => 'i', 'ó' => 'o', 'ú' => 'u', 'Á' => 'A', 'É' => 'E', 'Í' => 'I', 'Ó' => 'O', 'Ú' => 'U', 'ñ' => 'n', 'Ñ' => 'N');
		foreach ($accents as $key => $value) {
			if (strstr($text, $key)) { 
				$text = str_replace($key, $value, $text);
			}
		}

		return $text;
	}

	public static function escapeString($param){
		return mysql_real_escape_string($param);
	}

	public static function DiscardPre($Text){
		$Text = str_replace("<pre>", "", $Text);
		$Text = str_replace("</pre>", "", $Text);
		return $Text;
	}
	
	public static function saveCurrentPageInSession($CurrentPage){
		FunctionsHelper::saveInSession("currentPage", $CurrentPage . ".php");
	}

	public static function saveInSession($title, $message){
		if (session_id() == "")
			session_start();
		$_SESSION[$title] = $message;
	}

	public static function getErrorInSession($title){
		if (session_id() == "")
			session_start();

		$error = '';

		if (isset($_SESSION[$title])){
			$error = $_SESSION[$title];
			unset ($_SESSION[$title]);
		}
		return $error;
	}

	public static function limitText($text, $limit){
		$text = trim($text);
		if (strlen($text) < $limit)
			return $text;

		return substr($text, 0, $limit) . '...';
	}
	
	public static function limitTextUntilSpace($text, $limit){
		$text = trim($text);
		$character = '';
		
		if (strlen($text) < $limit)
			return $text;

		$currentText = substr($text, 0, $limit);
		
		for($a = 0; $character != ' ' && $character != "," && $character != "."; $a++){
			$character = substr($text, $limit + $a, 1);
			$currentText .= $character;
		}
		return $currentText . '...';
	}

	public static function adaptDBFormatDate($date){
		$birthdate = explode('/', $date);
		return $birthdate[2] . '-' . $birthdate[1] . '-' . $birthdate[0];
	}

	public static function adaptListFormatDate($date){
		$birthdate = explode('-', $date);
		return $birthdate[2] . '/' . $birthdate[1] . '/' . $birthdate[0];
	}
	
	public static function ifEmpty($text){
		if (empty($text))
			return 'No hay informaci&oacute;n que visualizar.';
			
		return $text;
	}
	
	public static function createArrayFromText($text, $delimiter){
		$array = explode($delimiter, $text);
		foreach ($array as &$position)
			$position = trim($position);
		return $array;
	}
}

?>